#! /usr/bin/env python
#-*- coding: utf-8 -*-

import os

from libs import read


class Dict:

    def __init__(self, lang):
        self.lang = lang
        locales = os.path.join('.', 'locales', lang)
        self.path = os.path.join(locales, 'dict')
        self.grampath = os.path.join(locales, 'grammar')

        self.pronouns = read(os.path.join(self.path,
                                          'pronouns.txt')).splitlines()
        self.nouns = read(os.path.join(self.path,
                                       'nouns.txt')).splitlines()
        self.modal = read(os.path.join(self.path,
                                       'modal-verbs.txt')).splitlines()
        self.verbs = read(os.path.join(self.path,
                                       'verbs.txt')).splitlines()
        self.adverbs = read(os.path.join(self.path,
                                         'adverbs.txt')).splitlines()
        self.prep = read(os.path.join(self.path,
                                      'preps.txt')).splitlines()
        verbs_i = [x.split() for x\
                   in read(os.path.join(self.path,
                                        'verbs-irregular.txt')).splitlines()]
        self.verbs_i = {}
        for i in verbs_i:
            self.verbs_i[i[1]] = i[0]
            self.verbs_i[i[2]] = i[0]

    def check_verb(self, w):
        i = self.verbs_i.values()
        if w in self.verbs:
            return 1
        elif w in i:
            return 1
        elif w[-1] is 'd':                    # Past form
            if w[:-1] in self.verbs:
                return 1
            if w[-2] is 'e':
                if w[:-2] in self.verbs:
                    return 1
                elif w[:-2] in i:
                    return 1
        elif w[-1] is 's':                    # Present simple
            if w[:-1] in self.verbs:
                return 1
            elif w[:-1] in i:
                return 1
            elif w[-2] is 'e':
                if w[:-2] in self.verbs:
                    return 1
                elif w[:-2] in i:
                    return 1
        elif w[-3:] == 'ing':                # Present continous
            if w[:-3] in self.verbs:
                return 1
            elif w[:-3] + 'e' in self.verbs:
                return 1
            elif len(w) > 5 and w[-4] == w[-5] and w[:-4] in self.verbs:
                return 1
        return 0

    def check_modal(self, w):
        if w in self.modal:
            return 1
        else:
            return 0

    def check_noun(self, w):
        if w in self.nouns:
            return 1
        elif w[-1] is 's':
            if w[:-1] in self.nouns:
                return 1
            elif w[-2] is 'e':
                if w[:-2] in self.nouns:
                    return 1
        return 0

    def check_pronoun(self, w):
        if w in self.pronouns:
            return 1
        return 0

    def check_adverb(self, w):
        if w in self.adverbs:
            return 1
        return 0

    def check_prep(self, w):
        if w in self.prep:
            return 1
        return 0

    def __getitem__(self, w):
        types = []
        if w in self.verbs_i.keys():
            types.append('verb')
        if self.check_noun(w):
            types.append('noun')
        if self.check_pronoun(w):
            types.append('pronoun')
        if self.check_adverb(w):
            types.append('adverb')
        if self.check_prep(w):
            types.append('prep')
        if self.check_verb(w) \
           or w in self.verbs_i.keys() \
           or self.check_modal(w):
            types.append('verb')
        if types == []:
            types.append('ukwn')
        return types
